package day26;
import java.sql.*;
import java.text.SimpleDateFormat;

/**
 * @author oldliu
 * @since 1.0
 */
public class DBUtil {
    public static final String clsName="com.mysql.cj.jdbc.Driver";
    public static final String url="jdbc:mysql://localhost:3306/myschool?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
    public static final String user="root";
    public static final String pass="341227261x";
    private DBUtil(){
    }
    static{
        try {
            Class.forName(clsName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, pass);
    }


    public static void  close(ResultSet res, Statement sta,Connection con)  {
        close(res);
        close(sta);
        close(con);
    }
    public static void close(ResultSet rs){
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement st){
        if(st!=null) {
            try {
               //st.setMaxRows(0);
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Connection con){
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    }
    /**
     * str转sql.date方法
     *
     * @param
     * @return
     */

    public static String StringToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("日期格式不正确");
        }
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        return sqlDate;
    }

}
